﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NLBS.Control;
using NLBS.Data;
using NLBS.Entity;

namespace NLBS.Admin.control
{
    public partial class userGroup : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            checkAdmin();
            if (!IsPostBack)
            {
              userGroupView.ItemDataBound += new RepeaterItemEventHandler(userGroupView_ItemDataBound);
              userGroupView.DataSource=DbProvider.Instance().GetUserGroup();
              userGroupView.DataBind();
              
            }
        }
       
        void userGroupView_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            
            
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                UserGroup group = (UserGroup)e.Item.DataItem;
                string right = group.Rights;
                //int groupView =int.Parse(right.Substring(0, 1));
                //int groupPost=int.Parse(right.Substring(1, 1));
                //int groupEdit = int.Parse(right.Substring(2, 1));
                //int groupDelete = int.Parse(right.Substring(3, 1));
                //int groupUpload = int.Parse(right.Substring(4, 1));
                HtmlInputCheckBox groupSelect = (HtmlInputCheckBox)e.Item.FindControl("groupSelect");
                groupSelect.Value = group.GroupID.ToString();
                if (group.GroupID <= 4)
                {
                    groupSelect.Disabled = true;
                }
                HtmlSelect view = (HtmlSelect)e.Item.FindControl("view");
                HtmlSelect post = (HtmlSelect)e.Item.FindControl("post");
                HtmlSelect edit = (HtmlSelect)e.Item.FindControl("edit");
                HtmlSelect delete = (HtmlSelect)e.Item.FindControl("delete");
                HtmlSelect upload = (HtmlSelect)e.Item.FindControl("upload");

                SelectGroup(view,group.Power.View);
                SelectGroup(post,group.Power.Post);
                SelectGroup(edit, group.Power.Edit);
                SelectGroup(delete, group.Power.Delete);
                SelectGroup(upload, group.Power.Upload);
            }
        }

        public void SelectGroup(HtmlSelect select, int id)
        {
            ListItemCollection items = select.Items;
            for (int i = 0; i < items.Count; i++)
            {
                if (id == int.Parse(items[i].Value))
                {
                    items[i].Selected = true;
                }
                else if (id > int.Parse(items[items.Count - 1].Value))
                {
                    items[items.Count - 1].Selected = true;
                }
            }
        }


    }
}